package org.Lin.service;

import org.Lin.dao.AdminDAO;
import org.Lin.dao.UserDAO;
import org.Lin.model.Admin;
import org.Lin.model.User;
import org.Lin.util.LogUtil;

import java.sql.SQLException;

public class AdminService {

    private final AdminDAO adminDAO = new AdminDAO();
    private final UserDAO userDAO = new UserDAO();

    public Admin loginAdmin(int adminID, String password) {
        try {
            Admin admin = adminDAO.findByAdminID(adminID);
            if (admin != null && admin.getAdminPassword().equals(password)) {
                LogUtil.logOperation("Admin", "login", "管理员登录: " + adminID);
                return admin;
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return null;
    }

    public boolean deleteUser(int userID, int adminID) {
        try {
            Admin admin = adminDAO.findByAdminID(adminID);
            if (admin == null || !admin.isSuperAdmin()) {
                return false; // 权限不足
            }
            userDAO.delete(userID);
            LogUtil.logOperation("User", "delete", "删除用户: " + userID);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean changeUserStatus(int targetUserID, String newStatus, int adminID) {
        try {
            Admin admin = adminDAO.findByAdminID(adminID);
            if (admin == null) {
                return false;
            }
            User user = userDAO.findByUserID(targetUserID);
            if (user == null) {
                return false;
            }
            user.setAccountStatus(newStatus);
            userDAO.update(user);
            LogUtil.logOperation("User", "change_status", "修改用户状态: " + targetUserID + " -> " + newStatus);
            return true;
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }
}
